#ifndef ____ADDR_HPP____
#define ____ADDR_HPP____

#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

class Inet_Addr
{
private:
    void Host2Net()
    {
        memset(&_addr, 0, sizeof(_addr));
        _addr.sin_addr.s_addr = inet_addr(_ip.c_str());
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(_port);
    }

    void Net2Host()
    {
        _port = ntohs(_addr.sin_port);
        _ip = inet_ntoa(_addr.sin_addr);
    }

public:

    Inet_Addr()
    {}

    void ReInit(const struct sockaddr_in& _peer)
    {
        _addr = _peer;
        Net2Host();
    }


    Inet_Addr(uint16_t port, std::string ip = "0.0.0.0")
        : _ip(ip), _port(port)
    {
        Host2Net();
    }

    Inet_Addr(const struct sockaddr_in& addr)
        :_addr(addr)
    {
        Net2Host();
    }


    std::string Ip()
    {
        return _ip;
    }

    uint16_t Port()
    {
        return _port;
    }

    std::string Get()
    {
        return " " + _ip + ":" + std::to_string(_port);
    }


    struct sockaddr* Addr()
    {
        return (struct sockaddr*)&_addr;
    }


    socklen_t Size()
    {
        return sizeof(_addr);
    }

    ~Inet_Addr()
    {}


    bool operator==(const Inet_Addr& _ina)
    {
        return _ip == _ina._ip && _port == _ina._port;
    }

private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};

#endif



